package team.seekers.hula.course.resource.sectionTalk.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.enums.WindTypeEnum;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.course.resource.sectionTalk.entity.dao.SectionTalkDO;
import team.seekers.hula.course.resource.sectionTalk.entity.dto.SectionTalkAddDTO;
import team.seekers.hula.course.resource.sectionTalk.entity.dto.SectionTalkQueryDTO;
import team.seekers.hula.course.resource.sectionTalk.entity.dto.SectionTalkUpdateDTO;
import team.seekers.hula.course.resource.sectionTalk.entity.vo.SectionTalkListVO;
import team.seekers.hula.course.resource.sectionTalk.mapper.SectionTalkMapper;
import team.seekers.hula.course.structure.section.service.SectionService;
import team.seekers.hula.message.message.service.MessageService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;
import team.seekers.hula.record.wind.service.WindService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class SectionTalkService extends ServiceImpl<SectionTalkMapper, SectionTalkDO> {

    @Autowired
    @Lazy
    private SectionService sectionService;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    @Lazy
    private MessageService messageService;

    @Autowired
    private WindService windService;

    public PageResult<SectionTalkListVO> pageOrderByGood(SectionTalkQueryDTO queryDTO) {
        LambdaQueryChainWrapper<SectionTalkDO> lambdaQuery = lambdaQuery();

        if(!StringUtils.isEmpty(queryDTO.getBegin())){
            lambdaQuery.ge(SectionTalkDO::getGmtCreate,queryDTO.getBegin());
        }
        if(!StringUtils.isEmpty(queryDTO.getEnd())){
            lambdaQuery.le(SectionTalkDO::getGmtCreate,queryDTO.getEnd());
        }

        lambdaQuery.orderByDesc(SectionTalkDO::getGood);

        Page<SectionTalkDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<SectionTalkDO> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<SectionTalkListVO> list = new ArrayList<>();
        for (SectionTalkDO record : records) {
            SectionTalkListVO sectionTalkListVO = new SectionTalkListVO();
            BeanUtils.copyProperties(record,sectionTalkListVO);
            UserDO userDO = userService.getById(record.getUserId());
            if(userDO == null){
                return null;
            }
            if(!record.getUserId().equals(MindUtils.getUserId())){
                sectionTalkListVO.setIsMe(true);
            }
            sectionTalkListVO.setUserInfo(userService.getFollowInfoVO(record.getUserId(),MindUtils.getUserId()));
            list.add(sectionTalkListVO);
        }
        return new PageResult<>(total,list);
    }

    public String add(SectionTalkAddDTO addDTO) {

        // 新增评论
        SectionTalkDO sectionTalkDO = new SectionTalkDO();
        BeanUtils.copyProperties(addDTO, sectionTalkDO);
        sectionTalkDO.setUserId(MindUtils.getUserId());
        sectionTalkDO.setCourseId(sectionService.getCourseIdById(addDTO.getSectionId()));
        save(sectionTalkDO);

        // 新增wind
        windService.addWind(WindTypeEnum.ADD_TALK,sectionTalkDO.getId(),2);

        return sectionTalkDO.getId();
    }

    public void delete(String id) {
        // 判断是不是该用户的评论
        SectionTalkDO commentDO = getById(id);
        if(!MindUtils.isAdmin()){
            if(!commentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }

        // 删除评论
        removeById(id);

        // 删除wind
        windService.removeWind(WindTypeEnum.ADD_TALK,id);
    }

    public void update(SectionTalkUpdateDTO updateDTO) {
        // 判断是不是该用户的评论
        SectionTalkDO answerCommentDO = getById(updateDTO.getId());
        if(!MindUtils.isAdmin()){
            if(!answerCommentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }
        //更新评论
        BeanUtils.copyProperties(updateDTO, answerCommentDO);
        updateById(answerCommentDO);
    }

    public void deleteBySectionId(String sectionId) {
        lambdaUpdate().eq(SectionTalkDO::getSectionId,sectionId).remove();
    }

    public void addGood(String sectionTalkId) {
        Integer good = lambdaQuery().select(SectionTalkDO::getGood).eq(SectionTalkDO::getId, sectionTalkId).one().getGood();
        lambdaUpdate().set(SectionTalkDO::getGood,good+1).eq(SectionTalkDO::getId, sectionTalkId).update();
    }

    public void addBad(String sectionTalkId) {
        Integer bad = lambdaQuery().select(SectionTalkDO::getBad).eq(SectionTalkDO::getId, sectionTalkId).one().getBad();
        lambdaUpdate().set(SectionTalkDO::getBad,bad+1).eq(SectionTalkDO::getId, sectionTalkId).update();
    }

    public SectionTalkListVO get(String id) {
        SectionTalkListVO talkListVO = new SectionTalkListVO();
        SectionTalkDO sectionTalkDO = getById(id);
        BeanUtils.copyProperties(sectionTalkDO,talkListVO);
        talkListVO.setUserInfo(userService.getFollowInfoVO(sectionTalkDO.getUserId(),MindUtils.getUserId()));
        if(!sectionTalkDO.getUserId().equals(MindUtils.getUserId())){
            talkListVO.setIsMe(true);
        }
        return talkListVO;
    }

    public List<SectionTalkListVO> listSome(String sectionId) {
        List<SectionTalkDO> sectionTalkDOList = lambdaQuery().eq(SectionTalkDO::getSectionId, sectionId).list();

        ArrayList<SectionTalkListVO> listVOS = new ArrayList<>();
        for (SectionTalkDO sectionTalkDO : sectionTalkDOList) {
            SectionTalkListVO talkListVO = new SectionTalkListVO();
            BeanUtils.copyProperties(sectionTalkDO,talkListVO);
            listVOS.add(talkListVO);
        }

        return listVOS;
    }
}
